package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    public BrandServiceImpl(){
        log.debug("创建业务对象：BrandServiceImpl");
    }

    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO){

        String brandName = brandAddNewDTO.getName();
        int count = brandMapper.countByName(brandName);
        if (count > 0) {
            String message = "添加品牌功能失败，尝试添加的品牌名称已经被使用！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }



        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        brandMapper.insert(brand);
    }



}

